import { createApp } from "vue";
import App from "@/App.vue";
import "@/style.css";
// --- UI 框架 ---
import ElementPlus from "element-plus";
import "element-plus/dist/index.css";
import zhCn from "element-plus/es/locale/lang/zh-cn"; // 中文语言包
import * as ElementPlusIconsVue from "@element-plus/icons-vue";

// --- 状态 & 路由 ---
import router from "@/router";
import pinia from "@/store";
import { useUserStore } from "@/store/userStore";

const app = createApp(App);

// 使用插件（推荐顺序：状态 → 路由 → UI）
app.use(pinia);
app.use(router);
app.use(ElementPlus, {
  locale: zhCn,
  size: "default",
});

// 注册所有 Element Plus 图标为全局组件
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}

// 应用启动时验证token
const initApp = async () => {
  const userStore = useUserStore();

  // 如果有token，尝试获取用户信息
  if (userStore.hasToken) {
    try {
      await userStore.fetchUserInfo();
      console.log("用户信息获取成功:", userStore.getName);
    } catch (error) {
      console.warn("获取用户详细信息失败，但保持登录状态:", error);
      // 不要清除登录状态，只是获取详细信息失败
    }
  }

  app.mount("#app");
};

initApp();
